import {observable, extendObservable, action, toJS} from 'mobx';
import {getCardInfo, deleteCardInfo} from 'util/api';
import _ from 'lodash';
import {validateFields} from '../Common.js';
import {message} from 'antd';
import store from 'store';

//导出
export default class CardUsedStore {
  constructor() {
    this.reset(true);
  }

  @action
  reset = init => {
    const state = {
      tableData: [],
      loading: false,
      tableParams: {},
      selectedRowKeys: [],
      filter_cardType: undefined,
      filter_household: '',
    };

    if (init) {
      extendObservable(this, state);
    } else {
      Object.keys(state).forEach(key => (this[key] = state[key]));
    }
  };
  @action
  changeCardType = async value => {
    this.ms_cardType = value;
  };
  @action
  changeHousehold = async value => {
    this.ms_household = value;
  };
  @action
  onSelectChange = async selectedRowKeys => {
    this.selectedRowKeys = selectedRowKeys;
  };
  @action
  search = async () => {
    let cardType = this.ms_cardType;
    let household = this.ms_household;
    this.tableParams = {pageNo: 1, pageSize: 10, ...this.tableParams, cardType};
    this.filter_cardType = cardType;
    this.filter_household = household;
    this.getCardInfo(this.tableParams);
  };

  @action
  onWillMount = async (location, match, history) => {
    this.reset();

    this.loading = true;
    await this.getCardInfo();
    this.loading = false;
  };

  @action
  getCardInfo = async (params = {pageNo: 1, pageSize: 10}) => {
    params['typeId'] = 2;
    this.loading = true;
    const res = await getCardInfo(params);
    this.loading = false;
    if (res.result === 'success') {
      this.tableParams = {...this.tableParams, ...params};
      this.tableData = res;
    } else {
      message.error(res.msg);
    }
  };

  @action
  onDelete = async record => {
    if (!toJS(this.selectedRowKeys)[0]) {
      message.error('请选择卡!');
      return;
    }
    let res = await deleteCardInfo({
      cardIdList: toJS(this.selectedRowKeys),
    });
    if (res.result === 'success') {
      this.getCardInfo();
      this.selectedRowKeys = [];
    } else {
      message.error(res.msg);
    }
  };
}
